﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
#if COUNT_DRAW_OPERATIONS
using BrainTechLLC;
using BrainTechLLC.ThreadSafeObjects;
#endif

namespace BrainTechLLC.ImageOperatorsNonAForgeDependent
{
    public static partial class Scaling
    {
        /// <summary>
        /// Quick way to determine multiplier for image width/height to ensure it fits Settings.Values.MinScaleWidth/Height
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static double GetScaleFactor(double width, double height, double minScaleWidth, double minScaleHeight, double minScaleFactor)
        {
            double minWidth = minScaleWidth;
            double minHeight = minScaleHeight;

            double scale = 1.0D;

            while (width * scale > minWidth && height * scale > minHeight)
            {
                scale -= 0.0125;
            }

            double result = Math.Max(minScaleFactor, scale);
            return result;
        }

        public static double GetScaledImageDimensions(double currentImageWidth, double currentImageHeight, double desiredImageWidth, double desiredImageHeight)
        {
            if (currentImageHeight > currentImageWidth)
            {
                return (desiredImageHeight > desiredImageWidth) ? desiredImageWidth / currentImageWidth : desiredImageHeight / currentImageHeight;
            }
            else
            {
                return (desiredImageHeight > desiredImageWidth) ? desiredImageWidth / currentImageWidth : desiredImageHeight / currentImageHeight;
            }
        }

        public static void GetScaledSize(int x, int y, int maxX, int maxY, out int newX, out int newY)
        {
            double outX, outY;
            GetScaledSize((double)x, (double)y, (double)maxX, (double)maxY, out outX, out outY);
            try
            {
                newX = Convert.ToInt32(Math.Floor(outX));
                newY = Convert.ToInt32(Math.Floor(outY));
            }
            catch (OverflowException)
            {
                newX = x;
                newY = y;
            }
        }

        /// <summary>
        /// Slow boat method to scale image to fit a specific max X and Y size
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="maxX"></param>
        /// <param name="maxY"></param>
        /// <param name="newX"></param>
        /// <param name="newY"></param>
        public static void GetScaledSize(double x, double y, double maxX, double maxY, out double newX, out double newY)
        {
            double subtractPerX = y / x;
            while (x > maxX || y > maxY)
            {
                x -= 1.0D;
                y -= subtractPerX;
            }
            newX = x;
            newY = y;
        }
    }
}
